/*
 * MyGWT User Extensions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.mygwt.ux.client.widgets;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
 * Extends the default GWT CheckBox. It can be used as a direct replacement to
 * GWT CheckBox.
 * 
 * The enhanced capabilities include the possibility to get and set the value
 * attribute. Plus for subclasses, it is possible to get the input Element.
 * 
 * @author Damien Ferrand
 */
public class CheckBox extends com.google.gwt.user.client.ui.CheckBox {
  private Element mInputElement;

  /**
   * @see com.google.gwt.user.client.ui.CheckBox#ChheckBox()
   */
  public CheckBox() {
    this(DOM.createInputCheck());
    setStyleName("gwt-CheckBox");
  }

  /**
   * @see com.google.gwt.user.client.ui.CheckBox#ChheckBox(Element)
   */
  protected CheckBox(Element elem) {
    super(elem);
    mInputElement = elem;
  }

  /**
   * @see com.google.gwt.user.client.ui.CheckBox#ChheckBox(String, boolean))
   */
  public CheckBox(String label, boolean asHTML) {
    this();
    if (asHTML) {
      setHTML(label);
    } else {
      setText(label);
    }
  }

  /**
   * @see com.google.gwt.user.client.ui.CheckBox#ChheckBox(String)
   */
  public CheckBox(String label) {
    this();
    setText(label);
  }

  /**
   * Sets the value attribute of the input Element.
   * 
   * @param pValue the "value" attribute value
   */
  public void setValue(String pValue) {
    DOM.setElementProperty(mInputElement, "value", pValue);
  }

  /**
   * Gets the value attribute of the input Element.
   * 
   * @return the value of the "value" attribute.
   */
  public String getValue() {
    return DOM.getElementProperty(mInputElement, "value");
  }

  /**
   * Get the input element of the checkbox
   * 
   * @return the input Element of the checkbox
   */
  protected Element getInputElement() {
    return mInputElement;
  }
}